package com.software.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.software.lease.model.entity.*;
import com.software.lease.model.enums.ItemType;
import com.software.lease.model.enums.ReleaseStatus;
import com.software.lease.web.admin.mapper.RoomInfoMapper;
import com.software.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.lease.web.admin.vo.attr.AttrValueVo;
import com.software.lease.web.admin.vo.graph.GraphVo;
import com.software.lease.web.admin.vo.room.RoomDetailVo;
import com.software.lease.web.admin.vo.room.RoomItemVo;
import com.software.lease.web.admin.vo.room.RoomQueryVo;
import com.software.lease.web.admin.vo.room.RoomSubmitVo;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomFacilityService facilityService;

    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;
    @Autowired
    private RoomAttrValueService attrValueService;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    /***
     * 保存或修改房间信息
     * @param roomSubmitVo
     */
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();
        if(isUpdate){
            //1.删除图片列表
            LambdaUpdateWrapper<GraphInfo> graphWrapper = new LambdaUpdateWrapper<>();
            graphWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
            graphWrapper.eq(GraphInfo::getItemId,roomId);
            graphInfoService.remove(graphWrapper);
            //2.删除属性列表
            LambdaUpdateWrapper<RoomAttrValue> attrValueWrapper = new LambdaUpdateWrapper<>();
            attrValueWrapper.eq(RoomAttrValue::getRoomId,roomId);
            attrValueService.remove(attrValueWrapper);
            //3.删除配套列表
            LambdaUpdateWrapper<RoomFacility> facilityWrapper = new LambdaUpdateWrapper<>();
            facilityWrapper.eq(RoomFacility::getRoomId,roomId);
            facilityService.remove(facilityWrapper);
            //4.删除标签列表
            LambdaUpdateWrapper<RoomLabel> labelWrapper = new LambdaUpdateWrapper<>();
            labelWrapper.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(labelWrapper);
            //5.删除支付方式列表
            LambdaUpdateWrapper<RoomPaymentType> paymentTypeWrapper = new LambdaUpdateWrapper<>();
            paymentTypeWrapper.eq(RoomPaymentType::getRoomId,roomId);
            paymentTypeService.remove(paymentTypeWrapper);
            //6.删除租期列表
            LambdaUpdateWrapper<RoomLeaseTerm> leaseTermWrapper = new LambdaUpdateWrapper<>();
            leaseTermWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
            leaseTermService.remove(leaseTermWrapper);
        }

        //插入图片列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoService.save(graphInfo);
            }
        }
        //插入属性列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomId);
                roomAttrValue.setAttrValueId(attrValueId);
                attrValueService.save(roomAttrValue);
            }
        }
        //3.插入配套列表
        List<Long> facilityIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityIds)){
            for (Long facilityId : facilityIds) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(roomId);
                roomFacility.setFacilityId(facilityId);
                facilityService.save(roomFacility);
            }
        }
        //4.插入标签列表
        List<Long> labelIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            for (Long labelId : labelIds) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setRoomId(roomId);
                roomLabel.setLabelId(labelId);
                roomLabelService.save(roomLabel);
            }
        }
        //5.插入支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(roomId);
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                paymentTypeService.save(roomPaymentType);
            }
        }
        //6.插入租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setRoomId(roomId);
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                leaseTermService.save(roomLeaseTerm);
            }
        }
    }

    /**
     * 根据id查询房间信息
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        System.out.println("roomInfo = " + roomInfo);
        //查询所属公寓信息
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.selectById(apartmentId);
        System.out.println("apartmentInfo = " + apartmentInfo);

        //获取相关属性信息
        List<GraphVo> graphVos = graphInfoService.selectByItemId(id, ItemType.ROOM);
        List<LabelInfo> labelInfos = roomLabelService.selectById(id,ItemType.ROOM);
        List<FacilityInfo> facilityInfos = facilityService.selectById(id);
        List<PaymentType> paymentTypes = paymentTypeService.selectById(id);
        List<LeaseTerm> leaseTerms = leaseTermService.selectById(id);
        List<AttrValueVo> attrValueVos = attrValueService.selectById(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setAttrValueVoList(attrValueVos);


        return roomDetailVo;
    }

    @Override
    public void removeByRoomId(Long roomId) {
        //1.删除图片列表
        LambdaUpdateWrapper<GraphInfo> graphWrapper = new LambdaUpdateWrapper<>();
        graphWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphWrapper.eq(GraphInfo::getItemId,roomId);
        graphInfoService.remove(graphWrapper);
        //2.删除属性列表
        LambdaUpdateWrapper<RoomAttrValue> attrValueWrapper = new LambdaUpdateWrapper<>();
        attrValueWrapper.eq(RoomAttrValue::getRoomId,roomId);
        attrValueService.remove(attrValueWrapper);
        //3.删除配套列表
        LambdaUpdateWrapper<RoomFacility> facilityWrapper = new LambdaUpdateWrapper<>();
        facilityWrapper.eq(RoomFacility::getRoomId,roomId);
        facilityService.remove(facilityWrapper);
        //4.删除标签列表
        LambdaUpdateWrapper<RoomLabel> labelWrapper = new LambdaUpdateWrapper<>();
        labelWrapper.eq(RoomLabel::getRoomId,roomId);
        roomLabelService.remove(labelWrapper);
        //5.删除支付方式列表
        LambdaUpdateWrapper<RoomPaymentType> paymentTypeWrapper = new LambdaUpdateWrapper<>();
        paymentTypeWrapper.eq(RoomPaymentType::getRoomId,roomId);
        paymentTypeService.remove(paymentTypeWrapper);
        //6.删除租期列表
        LambdaUpdateWrapper<RoomLeaseTerm> leaseTermWrapper = new LambdaUpdateWrapper<>();
        leaseTermWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
        leaseTermService.remove(leaseTermWrapper);
    }


    @Override
    public IPage<RoomItemVo> pageRoom(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        IPage<RoomItemVo> list =  roomInfoMapper.pageRoom(page,queryVo);
        return list;
    }

    @Override
    public List<RoomInfo> listByApartmentId(Long id) {
        List<RoomInfo> list = roomInfoMapper.listByApartmentId(id);
        return list;
    }
}




